package com.cos.lession48.web.controller;

import com.cos.lession48.common.Const;
import com.cos.lession48.entity.Emp;
import com.cos.lession48.services.EmpServices;
import com.cos.lession48.services.impl.EmpServicesImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: COS
 * @time: 2022/3/1 17:16
 * @description:
 */
@WebServlet("/emp")
public class EmpController extends BaseServlet {
    private EmpServices es = new EmpServicesImpl();

    public String login(HttpServletRequest request , HttpServletResponse response){
        //1 获取用户提交的数据
        String ename = request.getParameter("ename");
        String pwd = request.getParameter("pwd");
        //2 访问业务层
        Emp login = es.login(ename, pwd);
        if (login != null) {
            System.out.println("登录不成");
            request.getSession().setAttribute("is_login", login);
            return Const.REDIRECT+"emp?type=getAll";
        }
        request.setAttribute("msg", "用户名或密码有误");
        return Const.FORWARD+"login.jsp";
    }

    public String getAll(HttpServletRequest request , HttpServletResponse response){
        //登录判断
        Emp e = (Emp) request.getSession().getAttribute("is_login");
        // 判断用户为null就是没有登录
        if (e == null) {
            return Const.REDIRECT+"login.jsp";
        }
        System.out.println("这个获取数据的");
        //访问业务层获取数据
        List<Emp> empList =  es.getAll();
        //reqeust
        request.setAttribute("empList",empList);
        //转发
        return Const.FORWARD+"index.jsp";
    }

}
